#include "GLWorld.h"
#include "DarkGDK.h"
#include "GLACRobot.h"

#define GLW_OBJID_LAND 100
#define GLW_OBJID_OBJ1 101
#define GLW_OBJID_OBJ2 102
#define GLW_OBJID_OBJ3 103

// World size
#define GLW_WORLD_WIDTH  2000
#define GLW_WORLD_HEIGHT 2000
#define GLW_WORLD_HALF_W 1000
#define GLW_WORLD_HALF_H 1000

// Mouse button code
#define GLW_MOUSE_BUT1 0x0001
#define GLW_MOUSE_BUT2 0x0002
#define GLW_MOUSE_BUT3 0x0004
#define GLW_MOUSE_BUT4 0x0008

// Keyboard scancode
#define GLW_SCANCODE_1 0x02
#define GLW_SCANCODE_2 0x03
#define GLW_SCANCODE_3 0x04
#define GLW_SCANCODE_4 0x05
#define GLW_SCANCODE_5 0x06
#define GLW_SCANCODE_6 0x07
#define GLW_SCANCODE_7 0x08
#define GLW_SCANCODE_8 0x09
#define GLW_SCANCODE_9 0x0A
#define GLW_SCANCODE_0 0x0B
#define GLW_SCANCODE_MINUS 0x0C
#define GLW_SCANCODE_PLUS 0x0D

#define GLW_SCANCODE_TAB 0x0F

#define GLW_SCANCODE_Q 0x10
#define GLW_SCANCODE_W 0x11
#define GLW_SCANCODE_E 0x12

#define GLW_SCANCODE_A 0x1e
#define GLW_SCANCODE_S 0x1f
#define GLW_SCANCODE_D 0x20
#define GLW_SCANCODE_F 0x21

#define GLW_SCANCODE_Z 0x2C
#define GLW_SCANCODE_X 0x2D
#define GLW_SCANCODE_C 0x2E

#define GLW_SCANCODE_SPACE 0x39

#define GLW_SCANCODE_F 0x21 //open fog
#define GLW_SCANCODE_O 0x22 //close fog

GLWorld::GLWorld(void)
{
	m_nLandID = -1;
	m_lObjCount = 1;
}

GLWorld::~GLWorld(void)
{
	UninitWorldObjs();
}
int GLWorld::RunAFrame()
{
// 	float dx = dbObjectPositionX(GLW_OBJID_OBJ1);
// 	float dy = dbObjectPositionY(GLW_OBJID_OBJ1);
// 	float dz = dbObjectPositionZ(GLW_OBJID_OBJ1);
// 
// 	if ( dbLeftKey ( ) && dx>-GLW_WORLD_HALF_W )
// 		dbMoveObjectLeft ( GLW_OBJID_OBJ1,1 );
// 
// 	if ( dbRightKey ( ) && dx<GLW_WORLD_HALF_W  )
// 		dbMoveObjectRight ( GLW_OBJID_OBJ1,1 );
// 
// 	if ( dbUpKey ( ) && dy<GLW_WORLD_HALF_H )
// 		dbMoveObjectUp ( GLW_OBJID_OBJ1,1 );
// 
// 	if ( dbDownKey ( ) && dy>-GLW_WORLD_HALF_H )
// 		dbMoveObjectDown ( GLW_OBJID_OBJ1,1 );

	int dcx = dbMouseMoveX();
	int dcy = dbMouseMoveY();
	int dcz = dbMouseMoveZ();

	if(dbMouseClick() & GLW_MOUSE_BUT2)
	{
		dbHideMouse();
		dbZRotateCamera(dbCameraAngleZ()-(float)dcx/10);
		dbPitchCameraDown((float)dcy/10);
		dbPositionMouse(dbScreenWidth()/2,dbScreenHeight()/2);

		if(dbKeyState(GLW_SCANCODE_W))
			dbMoveCamera(2);

		if(dbKeyState(GLW_SCANCODE_S))
			dbMoveCamera(-2);

		if(dbKeyState(GLW_SCANCODE_A))
			dbMoveCameraLeft(0,2);

		if(dbKeyState(GLW_SCANCODE_D))
			dbMoveCameraRight(0,2);
	}
	else
	{
		dbShowMouse();

		// move tank
		if(dbKeyState(GLW_SCANCODE_W))
			m_pRobot->MoveAhead(2);

		if(dbKeyState(GLW_SCANCODE_S))
			m_pRobot->MoveBack(2);

		if(dbKeyState(GLW_SCANCODE_A))
			m_pRobot->TurnLeft(2);

		if(dbKeyState(GLW_SCANCODE_D))
			m_pRobot->TurnRight(2);
	}

	if(dbKeyState(GLW_SCANCODE_F))
	{
		dbFogOn();
	}
	if(dbKeyState(GLW_SCANCODE_O))
	{
		dbFogOff();
	}



	//dbYRotateCamera ( dbCameraAngleY() - dbMouseMoveY()/10 );
	//dbZRotateCamera ( dbCameraAngleZ() - dbMouseMoveZ()/10 );

	// cross
	dbInk(dbRGB(50, 200, 50), dbRGB(0, 0, 0));
	int sw = dbScreenWidth()/2;
	int sh = dbScreenHeight()/2;

	dbLine(sw - 6, sh - 1, sw - 1, sh - 1);
	dbLine(sw + 6, sh - 1, sw + 1, sh - 1);
	dbLine(sw - 6, sh + 1, sw - 1, sh + 1);
	dbLine(sw + 6, sh + 1, sw + 1, sh + 1);

	dbLine(sw - 1, sh - 6, sw - 1, sh - 1);
	dbLine(sw - 1, sh + 6, sw - 1, sh + 1);
	dbLine(sw + 1, sh - 6, sw + 1, sh - 1);
	dbLine(sw + 1, sh + 6, sw + 1, sh + 1);

	dbText(0, 12, "W,A,S,D to move camera.");
	dbText(0, 24, "Right click and drag mouse to rotate camera.");

	dbText(0, 36, dbHex(dbScanCode()));

// 	if(dbKeyState(GLW_SCANCODE_SPACE))
// 		Fire();

	GLPObjListItr itr = m_lstWorldObjs.begin();
	while ( itr != m_lstWorldObjs.end() )
	{
		IGLObjBase * pObj = *itr;
		pObj->TimeEscape(10);
		itr++;
	}


	return 0;
}

void GLWorld::InitWorldObjs()
{
	InitLand();
  	GLACRobot* pRobot = new GLACRobot();
	pRobot->SetPosition(0,0,0);
	m_lstWorldObjs.push_back( pRobot );
	m_pRobot = pRobot;

// 	GLACRobot* pRobot2 = new GLACRobot();
// 	pRobot2->SetPosition(50,0,0);
//  	m_lstWorldObjs.push_back( pRobot2 );
}

void GLWorld::UninitWorldObjs()
{
	if (m_nLandID>-1)
	{
		dbDeleteObject (m_nLandID);
	}

	while (!m_lstWorldObjs.empty())
	{
 		delete m_lstWorldObjs.back();
		m_lstWorldObjs.pop_back();
	}
}

void GLWorld::InitLand()
{
	m_nLandID = GetGDKOBJID();
	// Now the floor
	dbMakeObjectPlane		( m_nLandID , GLW_WORLD_WIDTH, GLW_WORLD_HEIGHT );
 //	dbXRotateObject			( nLandID , -90 );
	dbPositionObject		( m_nLandID , ( float ) 0 * 100 , 0 , ( float ) 0 * 100 );
	// 	dbTextureObject			( nLandID , 1 );
	// 	dbScaleObjectTexture	( nLandID , 2 , 2 );
	dbSetObjectCollisionOff ( m_nLandID );
	dbColorObject (m_nLandID,dbRGB(0,0,255) );

}

int GLWorld::GetNextObjID()
{
	return m_lObjCount++;
}
